﻿package lobby.popups
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import lobby.events.LobbyEvent;
	import flash.utils.*;
	import flash.ui.Keyboard;
	import lobby.exchangeddata.HashTable;
	
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)
	 * @author Nguyen Thi Bich Phuong (Daisy)
	 */
	public class MessagePopup extends MovieClip
	{
		public static const DELAY_TIME:int = 10000;
		public var titleTf:TextField;
		public var messageTf:TextField;
		public var textOkBtn:TextField;
		public var okBtn:SimpleButton;
		private var timeOutId:uint;
		private var msgCode:String;
		
		public function MessagePopup()
		{
			hide();
			textOkBtn.mouseEnabled = false;
			okBtn.addEventListener(MouseEvent.CLICK, hdlBtnOkClick);
			this.addEventListener(KeyboardEvent.KEY_DOWN, hdlPressEnter);
		}
		
		private function hdlPressEnter(e:KeyboardEvent):void 
		{
			if (e.keyCode == Keyboard.ENTER)
			{
				doOK();
			}
		}
		
		/**
		 * @author	Bamboo
		 * @param	e
		 */
		private function hdlBtnOkClick(e:MouseEvent):void
		{
			doOK();
		}
		
		/**
		 * @author	Tom
		 */
		private function doOK():void 
		{
			var event:LobbyEvent = new LobbyEvent(LobbyEvent.DIALOG_RESULT_OK, true);
			event.result = this.msgCode;
			dispatchEvent(event);
			clearTimeout(timeOutId);
			hide();
		}
		
		/**
		 * @author	Bamboo
		 */
		public function hide():void
		{
			//trace("hide");
			this.visible = false;
		}
		
		/**
		 * @author	Tom
		 */
		public function show():void
		{
			
			this.visible = true;
			setTimeout(setFocus, 100);
		}
		
		private function setFocus():void {
			stage.focus = titleTf;
		}
		/**
		 * @author	Tom
		 * @param	msg
		 */
		public function showMessage(msg:String):void
		{
			messageTf.text = msg;
			timeOutId = setTimeout(doOK, DELAY_TIME);
			show();
		}
		
		/**
		 * @author	Tom
		 * @param	hashTable
		 */
		public function changeTextLang(hashTable:HashTable):void 
		{
			this.titleTf.text = hashTable.lookupString("errMsg");
			this.textOkBtn.text = hashTable.lookupString("popupLoginOK");
		}
		
		/**
		 * @author	Tom
		 * @param	code
		 */
		public function set setMsgCode(code:String):void
		{
			this.msgCode = code;
		}
	}

}